package com.cjb.consumer.web;

import com.cjb.consumer.entity.UserDO;
import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.function.Supplier;

@RestController
@RequestMapping("/consumer/user")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    private Supplier<UserDO> userDOSupplier=UserDO::new;


    @HystrixCommand(fallbackMethod = "errorDetail")
   @RequestMapping("/saveUser")
   public Object saveUser(){
        UserDO userDO = new UserDO();
        userDO.setUsername("rest_user");
        userDO.setPassword("root");
        userDO.setSign("rest基于http格式调用");
        ResponseEntity<String> ret = restTemplate.postForEntity("http://usercenter/user/saveUser", userDO, String.class);
           return  ret.getBody();
   }


   public String errorDetail(){
       return "服务出错了哦！！！";
   }




   @HystrixCommand(fallbackMethod = "errorDetail")
 @RequestMapping("/listUsers")
 public String listUsers(){

          String ret = restTemplate.getForObject("http://usercenter/user/listUsers", String.class);
          return ret;

 }





}
